package com.ruoyi.finance.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.finance.domain.VoucherTemplateType;
import com.ruoyi.finance.vo.TemPlateDTO;
import com.ruoyi.finance.vo.VoucherDetailVo;
import com.ruoyi.finance.vo.VoucherTemplateVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.finance.domain.VoucherTemplate;
import com.ruoyi.finance.service.IVoucherTemplateService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 发票凭证模板Controller
 *
 * @author ruoyi
 * @date 2025-03-03
 */
@RestController
@RequestMapping("/finance/template")
@Slf4j
public class VoucherTemplateController extends BaseController
{
    @Autowired
    private IVoucherTemplateService voucherTemplateService;

    /**
     * 查询发票凭证模板列表
     */
    @PreAuthorize("@ss.hasPermi('finance:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(VoucherTemplate voucherTemplate)
    {
        startPage();
        List<VoucherTemplate> list = voucherTemplateService.selectVoucherTemplateList(voucherTemplate);
        return getDataTable(list);
    }

    @GetMapping("/tolist")
    public TableDataInfo tolist()
    {
       List<VoucherTemplateVO> tolist=voucherTemplateService.selectlist();
        log.info("列表列表列表........:"+tolist);
        return getDataTable(tolist);
    }

    //TODO: 模版类型
    @GetMapping("/toselectTemplateType")
    public AjaxResult toselectTemplateType()
    {
     List<VoucherTemplateType> toselectTemplateType=voucherTemplateService.toselectTemplateType();
     return AjaxResult.success(toselectTemplateType);
    }



    /**
     * 导出发票凭证模板列表
     */
    @PreAuthorize("@ss.hasPermi('finance:template:export')")
    @Log(title = "发票凭证模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VoucherTemplate voucherTemplate)
    {
        List<VoucherTemplate> list = voucherTemplateService.selectVoucherTemplateList(voucherTemplate);
        ExcelUtil<VoucherTemplate> util = new ExcelUtil<VoucherTemplate>(VoucherTemplate.class);
        util.exportExcel(response, list, "发票凭证模板数据");
    }

    /**
     * 获取发票凭证模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('finance:template:query')")
    @GetMapping(value = "/{templateId}")
    public AjaxResult getInfo(@PathVariable("templateId") Long templateId)
    {
        return success(voucherTemplateService.deleteVoucherTemplateByTemplateId(templateId));
    }

    /**
     * 新增发票凭证模板
     */
    @PreAuthorize("@ss.hasPermi('finance:template:add')")
    @Log(title = "发票凭证模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VoucherTemplate voucherTemplate)
    {
        return toAjax(voucherTemplateService.insertVoucherTemplate(voucherTemplate));
    }



    //TODO: 新增发票凭证模板
    @PostMapping("/toadd")
    public AjaxResult toadd(@RequestBody TemPlateDTO TemPlateDTO)
    {
        return toAjax(voucherTemplateService.toadd(TemPlateDTO));
    }


    /**
     * 修改发票凭证模板
     */
    @PreAuthorize("@ss.hasPermi('finance:template:edit')")
    @Log(title = "发票凭证模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VoucherTemplate voucherTemplate)
    {
        return toAjax(voucherTemplateService.updateVoucherTemplate(voucherTemplate));
    }

    /**
     * 删除发票凭证模板
     */
    @PreAuthorize("@ss.hasPermi('finance:template:remove')")
    @Log(title = "发票凭证模板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{templateIds}")
    public AjaxResult remove(@PathVariable Long[] templateIds)
    {
        return toAjax(voucherTemplateService.deleteVoucherTemplateByTemplateIds(templateIds));
    }
}
